import fetch from "node-fetch";
import * as cheerio from "cheerio";
import type { MirrorWebsiteSpiderResult } from "@common/spider-types";
import type { Element, Text } from "domhandler";

/**
 * 抓取镜像网站,解析出文件/文件夹列表(支持大多数镜像网站)
 * @param url 镜像地址
 */
export async function fetchMirrorWebsite(url: string): Promise<MirrorWebsiteSpiderResult[]> {
  const resp = await fetch(url);
  const html = await resp.text();
  const $ = cheerio.load(html);
  const aTags = $("pre a");
  const results: MirrorWebsiteSpiderResult[] = [];
  for (const aTag of aTags) {
    if (aTag.firstChild?.type === "text") {
      const result = parseItem(aTag);
      if (result) {
        results.push(result);
      }
    }
  }
  return results;
}

function parseItem(aTag: Element): MirrorWebsiteSpiderResult | undefined {
  try {
    let name = (aTag.firstChild as Text).nodeValue;
    // 匹配版本号: 1.xxx[.xxx][.xxx]
    const type = name.endsWith("/") ? "dir" : "file";
    name = type === "dir" ? name.substring(0, name.length - 1) : name;
    if (name === "..") return undefined;
    let lastModified: string | undefined;
    if (aTag.next?.type === "text") {
      const c = aTag.next.nodeValue.trim();
      const [date, time] = c.split(/\s+/);
      lastModified = `${date.trim()} ${time.trim()}`;
    }
    return { name, type, lastModified };
  } catch (e: any) {
    console.error(e);
  }
}
